package com.lixin.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {
    public static String generateToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim(KeyEnum.JWT_CLAIMS_KEY.getKey(), claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + TimeEnum.JWT_TOKEN_EXPIRE_TIME.getTime()))
                .sign(Algorithm.HMAC256(KeyEnum.JWT_SECRET_KEY.getKey()));
    }

    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(KeyEnum.JWT_SECRET_KEY.getKey()))
                .build().verify(token).getClaims().get(KeyEnum.JWT_CLAIMS_KEY.getKey()).asMap();
    }
}
